<!DOCTYPE html>
<html>
	<head>
		<title>附件管理</title>
		<meta charset="utf-8">
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css">

		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>

		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>

		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>

		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>

		<script src="../../static/sa.js"></script>
		<script src="../../sa-resources/config.js"></script>

		<style type="text/css">
			.demo-table-expand {
				font-size: 0;
			}

			.demo-table-expand label {
				width: 90px;
				color: #99a9bf;
			}

			.demo-table-expand .el-form-item {
				margin-right: 0;
				margin-bottom: 0;
				width: 100%;
				border-bottom: 1px #eeeeee solid;
			}

			.el-table .nomodify-row {
				background: #e2e2e2;
			}
		</style>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<div class="c-panel">
				<!-- ------------- 参数栏 ------------- -->
				<div class="c-title">检索参数</div>
				<el-form>
					<div class="c-item">
						<label class="c-label">文件名称：</label>
						<el-input size="mini" v-model="queryParams[0].right[0]" placeholder="模糊查询"></el-input>
					</div>

					<div class="c-item" style="min-width: 0px;">
						<el-button size="mini" type="primary" icon="el-icon-search" @click="params.page = 1; f5()">查询
						</el-button>
					</div>
				</el-form>

				<!-- ------------- 快捷按钮 ------------- -->
				<div class="fast-btn">
					<el-button size="mini" type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
					<el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteByIds()">批量删除</el-button>
					<el-button size="mini" type="info" icon="el-icon-refresh" @click="sa.f5()">重置</el-button>
				</div>

				<!-- ------------- 数据列表 ------------- -->
				<el-table :border="true" :data="dataList" size="small" ref="data-table" class="wuyan-table"
					row-key="id">
					<el-table-column align="center" type="selection" width="45px"></el-table-column>


					<el-table-column align="center" label="源文件名称" prop="sourceName"></el-table-column>

					<el-table-column align="center" label="访问路径-web" prop="url">
						<template slot-scope="s">
							<span style="cursor: pointer;color: #007AFF;"
								@click="window.open(s.row.url)">{{s.row.url}}</span>
						</template>
					</el-table-column>

					<el-table-column align="center" label="文件类型" prop="type"></el-table-column>

					<el-table-column align="center" label="文件类型名称" prop="typeName"></el-table-column>

					<el-table-column align="center" label="预览路径-web" prop="preUrl">
						<template slot-scope="s">
							<span style="cursor: pointer;color: #007AFF;"
								@click="window.open(s.row.preUrl)">{{s.row.preUrl}}</span>
						</template>
					</el-table-column>
					</el-table-column>

					<el-table-column align="center" label="文件大小" prop="size">
						<template slot-scope="s">
							<span>{{s.row.size / 1024}} Kb</span>
						</template>
					</el-table-column>

					<el-table-column align="center" label="上传者" prop="userId"></el-table-column>

					<el-table-column align="center" label="创建时间" prop="createTime">
						<template slot-scope="s">
							<span>{{timestampToTime(s.row.createTime)}}</span>
						</template>
					</el-table-column>

					<el-table-column align="center" label="操作" width="240px" fixed="right">
						<template slot-scope="s">
							<el-button class="c-btn" type="success" icon="el-icon-view" @click="get(s.row)">查看
							</el-button>
							<el-button class="c-btn" type="danger" icon="el-icon-delete" @click="del(s.row)"
								v-if="!s.row.cfgModify || s.row.cfgModify === 1">删除</el-button>
						</template>
					</el-table-column>

				</el-table>

				<!-- ------------- 分页 ------------- -->
				<div class="page-box">
					<el-pagination background layout="total, prev, pager, next, sizes, jumper"
						:current-page.sync="params.page" :page-size.sync="params.limit" :total="dataCount"
						:page-sizes="[15,30,50,100,500,1000,10000]" @current-change="f5()" @size-change="f5()">
					</el-pagination>
				</div>
			</div>

			<el-dialog title="上传附件" :visible.sync="dialogUploadVisible">
				<div style="width: 100%;text-align: center;">
					<el-upload class="upload-demo" drag :data="{authorization: user.token}" :action="URL_UPLOAD"
						:multiple="false" :on-success="() => {sa.ok('上传成功')}" :on-error="() => {sa.error('上传失败')}">
						<i class="el-icon-upload"></i>
						<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
						<div class="el-upload__tip" slot="tip">单文件大小建议不超过10Mb</div>
					</el-upload>
				</div>
			</el-dialog>
		</div>

		<script src="../../sa-resources/api.js"></script>
		<script src="../../sa-resources/wuyan.js"></script>
		<script src="../../sa-resources/element-ui-helper.js"></script>
		
		<script>
			var app = new Vue({
				el: '.vue-box',
				data: {
					// 请求参数
					params: {
						isPage: true,
						page: 1,
						limit: 15,
						orders: '[]',
						params: '[]',
					},
					// 查询参数
					queryParams: [{
						left: 'name',
						op: 'like',
						right: ['']
					}, ],
					// 排序参数
					orderParams: [],
					// 当前数据
					dataList: [],
					// 数据量
					dataCount: 0,
					// 当前选择条目-单项
					chooseData: {
						row: null,
						field: null,
					},
					dialogUploadVisible: false,
				},
				created: function() {
					axiosConfig(axios)
					sa.onInputEnter(); // 监听输入框的回车事件，执行查询  
				},
				mounted() {
					this.f5();
				},
				methods: {
					// 刷新
					f5: function() {
						let loading = elLoadding({
							that: this
						})

						this.params.params = queryParamsToJsonStr(this.queryParams)
						this.params.orders = JSON.stringify(this.orderParams)
						let url = getUrl('pub-file', 'list')
						let http = getHttp(axios, 'pub-file', 'list')
						http(url, {
								params: this.params
							})
							.then(res => {
								this.dataList = res.list ? res.list : []; // 数据
								this.dataCount = res.count; // 分页
								sa.f5TableHeight(); // 刷新表格高度 
								loading.close();
							})
							.catch((err) => { // 请求失败处理
								this.$notify.info({
									title: '消息',
									message: '初始化数据失败'
								});

								loading.close();
							});
					},

					// 新增
					add: function() {
						this.dialogUploadVisible = true
					},

					// 查看
					get(data) {
						window.open(data.url)
					},

					// 删除
					del: function(data) {
						let loading = elLoadding({
							that: this
						})

						sa.confirm('是否删除，此操作不可撤销', function() {
							let url = getUrl('pub-file', 'del', [data.id])
							let http = getHttp(axios, 'pub-file', 'del')
							http(url, {})
								.then(res => {
									this.f5()
									sa.ok('删除成功');

									loading.close()
								})
								.catch(function(err) {
									this.$notify.info({
										title: '消息',
										message: '操作失败'
									});

									loading.close()
								});
						}.bind(this));
					},

					// 批量删除
					deleteByIds: function() {
						// 获取选中元素的id列表
						let selection = this.$refs['data-table'].selection;
						let ids = sa.getArrayField(selection, 'id');
						if (selection.length === 0) {
							return sa.msg('请至少选择一条数据')
						}
						
						let loading = elLoadding({
							that: this
						})

						// 提交删除 
						sa.confirm('是否删除，此操作不可撤销', function() {
							let url = getUrl('pub-file', 'del', ids)
							let http = getHttp(axios, 'pub-file', 'del')
							http(url, {})
								.then(res => {
									this.f5()
									sa.ok('删除成功');

									loading.close()
								})
								.catch(function(err) { // 请求失败处理
									this.$notify.info({
										title: '消息',
										message: '操作失败'
									});

									loading.close()
								});
						}.bind(this));
					},
				}
			})
		</script>
	</body>
</html>
